package com.topscore.uke.mvp.presenters;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.topscore.uke.mvp.contractors.IndexPageFragmentContractor;
import com.topscore.uke.ui.fragments.ActivitiesPageFragment;
import com.topscore.uke.ui.fragments.CommodityPageFragment;
import com.topscore.uke.ui.fragments.HomePageFragment;
import com.topscore.uke.ui.fragments.MinePageFragment;

/**
 * Created by 71468 on 2017/9/12.
 */

public class IndexPageFragmentPresenter implements IndexPageFragmentContractor, IndexPageFragmentContractor.Presenter {

    private View mView;
    private FragmentManager mManager;
    private FragmentTransaction mTransaction;
    private int mContainerViewId;

    private int mCurrentPage = 100;

    private final int _PageHome = 0;
    private final int _PageCommodity = 1;
    private final int _PageActivities = 2;
    private final int _PageMine = 3;

    private HomePageFragment mHomePageFragment;
    private CommodityPageFragment mCommodityPageFragment;
    private ActivitiesPageFragment mActivitiesPageFragment;
    private MinePageFragment mMinePageFragment;

    private UpdateUserInfoPresenter mUserInfoPresenter;

    public IndexPageFragmentPresenter(View view, FragmentManager mManager, int containerViewId) {
        this.mView = view;
        this.mManager = mManager;
        this.mContainerViewId = containerViewId;
    }

    @Override
    public void onHomePage() {
        if (mCurrentPage != _PageHome) {
            mTransaction = mManager.beginTransaction();
            hideFragment(mTransaction);
            if (mHomePageFragment == null) {
                mHomePageFragment = new HomePageFragment();
                mTransaction.add(mContainerViewId, mHomePageFragment);
            } else {
                mTransaction.show(mHomePageFragment);
            }
            mTransaction.commit();
            mView.onClearSelected();
            mView.onSelectHome();
            mCurrentPage = _PageHome;
        }
    }

    @Override
    public void onCommodity() {
        if (mCurrentPage != _PageCommodity) {
            mTransaction = mManager.beginTransaction();
            hideFragment(mTransaction);
            if (mCommodityPageFragment == null) {
                mCommodityPageFragment = new CommodityPageFragment();
                mTransaction.add(mContainerViewId, mCommodityPageFragment);
            } else {
                mTransaction.show(mCommodityPageFragment);
            }
            mTransaction.commit();
            mView.onClearSelected();
            mView.onSelectCommodity();
            mCurrentPage = _PageCommodity;
        }
    }

    @Override
    public void onActivities() {
        if (mCurrentPage != _PageActivities) {
            mTransaction = mManager.beginTransaction();
            hideFragment(mTransaction);
            if (mActivitiesPageFragment == null) {
                mActivitiesPageFragment = new ActivitiesPageFragment();
                mTransaction.add(mContainerViewId, mActivitiesPageFragment);
            } else {
                mTransaction.show(mActivitiesPageFragment);
            }
            mTransaction.commit();
            mView.onClearSelected();
            mView.onSelectActivities();
            mCurrentPage = _PageActivities;
        }
    }

    @Override
    public void onMine() {
        if (mCurrentPage != _PageMine) {
            mTransaction = mManager.beginTransaction();
            hideFragment(mTransaction);
            if (mMinePageFragment == null) {
                mMinePageFragment = new MinePageFragment();
                mUserInfoPresenter = new UpdateUserInfoPresenter(mMinePageFragment);
                mTransaction.add(mContainerViewId, mMinePageFragment);
            } else {
                mTransaction.show(mMinePageFragment);
            }
            mTransaction.commit();
            mView.onClearSelected();
            mView.onSelectMine();
            mCurrentPage = _PageMine;
        }
        mUserInfoPresenter.updateUserInfo();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mHomePageFragment != null) {
            transaction.hide(mHomePageFragment);
        }
        if (mCommodityPageFragment != null) {
            transaction.hide(mCommodityPageFragment);
        }
        if (mActivitiesPageFragment != null) {
            transaction.hide(mActivitiesPageFragment);
        }
        if (mMinePageFragment != null) {
            transaction.hide(mMinePageFragment);
        }
    }
}
